<?php
/*
 * 数据表
 * @package    conf
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 837d60bba899d503f8463f9317c008c9ab997d67 $
 */
defined('IN_IXDCW') or exit('Access Denied');
class Table {

	const NAME_MODULE        = 'module';
	const NAME_CATEGORY      = 'category';
	const NAME_MEMBER        = 'member';
	const NAME_COMPANY       = 'company';
	const NAME_COMPANY_CATID = 'company_catid';
	const NAME_COMPANY_DATA  = 'company_data';
	const NAME_AREA          = 'area';
	const NAME_FIELDS        = 'fields';
	const NAME_QUOTE_PRODUCT = 'quote_product';
	const NAME_MEMBER_GROUP  = 'member_group';
	const NAME_TYPE          = 'type';
	const NAME_KEYLINK       = 'keylink';
	const NAME_SETTING       = 'setting';
	const NAME_BANIP         = 'banip';
	const NAME_BANWORD       = 'banword';
	const NAME_MESSAGE       = 'message'; // 站内信
	const NAME_COMMENT_BAN           = 'comment_ban';
	const NAME_AUTO_CATEGORY         = 'auto_category';
	const NAME_AUTO_COMMENT          = 'auto_comment';
	const NAME_AUTO_RANK_BRAND       = 'auto_rank_brand';
	const NAME_AUTO_RANK_CAR         = 'auto_rank_car';
	const NAME_AUTO_RANK_MANUFACT    = 'auto_rank_manufact';
	const NAME_AUTO_FIELDS           = 'auto_fields';
	const NAME_AUTO_SALE             = 'auto_sale'; //汽车批发
	const NAME_AUTO_MODEL            = 'auto_model';
	const NAME_AUTO_MODEL_PASSENGER  = 'auto_model_passenger';
	const NAME_AUTO_MODEL_COMMERCIAL = 'auto_model_commercial';
	const NAME_AUTO_MODEL_RV         = 'auto_model_rv';
	const NAME_AUTO_INSURANCE        = 'auto_insurance';
	const NAME_AUTO_INSURANCE_COMPANY= 'auto_insurance_company';
	const NAME_MAINTENANCE           = 'maintaince';
	const NAME_MAINTENANCE_DATA      = 'maintaince_data';
	const NAME_VIDEO                 = 'video';
	const NAME_VIDEO_DATA            = 'video_data';
	const NAME_AUTO_PICTURE          = 'auto_picture';
	const NAME_CITY                  = 'city';
	const NAME_INFORMATION           = 'information';
	const NAME_INFORMATION_DATA      = 'information_data';
	const NAME_GUIDE                 = 'guide';
	const NAME_GUIDE_DATA            = 'guide_data';
	const NAME_QUOTE                 = 'quote';
	const NAME_QUOTE_DATA            = 'quote_data';
	const NAME_OWNERS                = 'owners';
	const NAME_OWNERS_DATA           = 'owners_data';
	const NAME_MERCHANT_DOC          = 'merchant_doc';
	const NAME_MERCHANT_EDITION      = 'merchant_edition';
	const NAME_MERCHANT_CATID        = 'merchant_catid';
	const NAME_MERCHANT_DRAFT        = 'merchant_draft';
	const NAME_BUSINESS              = 'business';
	const NAME_BUSINESS_DATA         = 'business_data';
	const NAME_BUSINESS_AUTH         = 'business_auth';
	const NAME_BUSINESS_PICTURE      = 'business_picture';
	const NAME_REVIEW                = 'review';
    const NAME_PRICE                 ='price';
    const NAME_PRICE_DATA            ='price_data';
    const NAME_LOAN                = 'loan';
	/**
	 * 获取普通的表名称
	 * @param $nameType
	 * @return string
	 */
	public static function name($nameType) {
		$allTables = self::_getTables();
		if (isset($allTables[$nameType])) {
			return $allTables[$nameType];

		} else {
			userError('不正确的表名称:' . self::_getPre() . $nameType . __FILE__ . '.' . __CLASS__ . '.' . __LINE__, E_USER_ERROR);
			return '';
		}
	}

	/**
	 * 汽车图片表
	 * @param $carId
	 * @return string
	 */
	public static function autoPicture($carId) {
		return self::name(self::NAME_AUTO_PICTURE) . '_' . $carId % 10;
	}
    public static function loan() {
        return self::name(self::NAME_LOAN) . '_' . $carId % 10;
    }
    public static function price() {
        return self::name(self::NAME_PRICE) . '_' . $carId % 10;
    }

	private static function _getTables() {
		$tablePre = self::_getPre();
		return array(
			self::NAME_MODULE                => $tablePre . self::NAME_MODULE,
			self::NAME_CATEGORY              => $tablePre . self::NAME_CATEGORY,
			self::NAME_MEMBER                => $tablePre . self::NAME_MEMBER,
			self::NAME_COMPANY               => $tablePre . self::NAME_COMPANY,
			self::NAME_COMPANY_CATID         => $tablePre . self::NAME_COMPANY_CATID,
			self::NAME_COMPANY_DATA          => $tablePre . self::NAME_COMPANY_DATA,
			self::NAME_AREA                  => $tablePre . self::NAME_AREA,
			self::NAME_FIELDS                => $tablePre . self::NAME_FIELDS,
			self::NAME_QUOTE_PRODUCT         => $tablePre . self::NAME_QUOTE_PRODUCT,
			self::NAME_MEMBER_GROUP          => $tablePre . self::NAME_MEMBER_GROUP,
			self::NAME_TYPE                  => $tablePre . self::NAME_TYPE,
			self::NAME_KEYLINK               => $tablePre . self::NAME_KEYLINK,
			self::NAME_SETTING               => $tablePre . self::NAME_SETTING,
			self::NAME_BANIP                 => $tablePre . self::NAME_BANIP,
			self::NAME_BANWORD               => $tablePre . self::NAME_BANWORD,
			self::NAME_COMMENT_BAN           => $tablePre . self::NAME_COMMENT_BAN,
			self::NAME_AUTO_CATEGORY         => $tablePre . self::NAME_AUTO_CATEGORY,
			self::NAME_AUTO_COMMENT          => $tablePre . self::NAME_AUTO_COMMENT,
			self::NAME_AUTO_FIELDS           => $tablePre . self::NAME_AUTO_FIELDS,
			self::NAME_AUTO_SALE             => $tablePre . self::NAME_AUTO_SALE,
			self::NAME_AUTO_RANK_BRAND       => $tablePre . self::NAME_AUTO_RANK_BRAND,
			self::NAME_AUTO_RANK_MANUFACT    => $tablePre . self::NAME_AUTO_RANK_MANUFACT,
			self::NAME_AUTO_RANK_CAR         => $tablePre . self::NAME_AUTO_RANK_CAR,
			self::NAME_AUTO_MODEL            => $tablePre . self::NAME_AUTO_MODEL,
			self::NAME_AUTO_MODEL_PASSENGER  => $tablePre . self::NAME_AUTO_MODEL_PASSENGER,
			self::NAME_AUTO_MODEL_COMMERCIAL => $tablePre . self::NAME_AUTO_MODEL_COMMERCIAL,
			self::NAME_AUTO_MODEL_RV         => $tablePre . self::NAME_AUTO_MODEL_RV,
			self::NAME_AUTO_INSURANCE        => $tablePre . self::NAME_AUTO_INSURANCE,
			self::NAME_AUTO_INSURANCE_COMPANY=> $tablePre . self::NAME_AUTO_INSURANCE_COMPANY,
			self::NAME_MAINTENANCE           => $tablePre . self::NAME_MAINTENANCE,
			self::NAME_MAINTENANCE_DATA      => $tablePre . self::NAME_MAINTENANCE_DATA,
			self::NAME_VIDEO                 => $tablePre . self::NAME_VIDEO,
			self::NAME_VIDEO_DATA            => $tablePre . self::NAME_VIDEO_DATA,
			self::NAME_AUTO_PICTURE          => $tablePre . self::NAME_AUTO_PICTURE,
			self::NAME_CITY                  => $tablePre . self::NAME_CITY,
			self::NAME_INFORMATION           => $tablePre . self::NAME_INFORMATION,
			self::NAME_GUIDE                 => $tablePre . self::NAME_GUIDE,
			self::NAME_GUIDE_DATA            => $tablePre . self::NAME_GUIDE_DATA,
			self::NAME_QUOTE                 => $tablePre . self::NAME_QUOTE,
            self::NAME_QUOTE_DATA            => $tablePre . self::NAME_QUOTE_DATA,
			self::NAME_OWNERS                => $tablePre . self::NAME_OWNERS,
			self::NAME_OWNERS_DATA           => $tablePre . self::NAME_OWNERS_DATA,
			self::NAME_MESSAGE               => $tablePre . self::NAME_MESSAGE,
			self::NAME_MERCHANT_DOC          => $tablePre . self::NAME_MERCHANT_DOC,
			self::NAME_MERCHANT_EDITION      => $tablePre . self::NAME_MERCHANT_EDITION,
			self::NAME_MERCHANT_CATID        => $tablePre . self::NAME_MERCHANT_CATID,
			self::NAME_MERCHANT_DRAFT        => $tablePre . self::NAME_MERCHANT_DRAFT,
			self::NAME_BUSINESS              => $tablePre . self::NAME_BUSINESS,
			self::NAME_BUSINESS_AUTH         => $tablePre . self::NAME_BUSINESS_AUTH,
			self::NAME_BUSINESS_DATA         => $tablePre . self::NAME_BUSINESS_DATA,
			self::NAME_BUSINESS_PICTURE      => $tablePre . self::NAME_BUSINESS_PICTURE,
			self::NAME_REVIEW                => $tablePre . self::NAME_REVIEW,
            self::NAME_LOAN                => $tablePre . self::NAME_LOAN,
            self::NAME_PRICE               =>$tablePre.self::NAME_PRICE,
            self::NAME_PRICE_DATA            => $tablePre . self::NAME_PRICE_DATA
		);
	}

	/**
	 * 数据库前缀
	 * @return mixed
	 */
	private static function _getPre() {
		global $db;
		return $db->getPre();
	}

	/**
	 * 根据item的id值区分表的数据表的名称
	 * @param $moduleid
	 * @param $itemid
	 * @param $tableData
	 * @return string
	 */
	public static function getInfoTable($moduleid, $itemid, $tableData) {
		$MOD   = PCache::read("moduleid-$moduleid.php");
		$split = isset($MOD['split']) ? $MOD['split'] : 0;
		if ($split) {
			return $tableData . '_' . self::_splitInfoid($itemid);
		} else {
			return $tableData;
		}
	}

	/**
	 * 根据id返回分表的名称, 50W为信息分表处理方式
	 * @param $id
	 * @return float|int
	 */
	private static function _splitInfoid($id) {
		return $id > 0 ? ceil($id / 500000) : 1;
	}


	public static function upload($userid) {
		return self::_getPre() . 'upload_' . ($userid % 10);
	}

}